﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;
using IssueTracker.Business.Entity;

namespace IssueTracker.Data
{
    public class ProjectCustomTypeColumnDataAccess: DataAccessComponent
    {
        private IssueTrackerDBDataContext dc;

        public ProjectCustomTypeColumn Create(ProjectCustomTypeColumn projectCustomTypeColumn)
        {
            ProjectCustomTypeColumn temp = base.Clone < ProjectCustomTypeColumn >(projectCustomTypeColumn);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;

                dc.ProjectCustomTypeColumns.InsertOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();

                    projectCustomTypeColumn = base.Clone<ProjectCustomTypeColumn>(temp);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return projectCustomTypeColumn;
        }

        public ProjectCustomTypeColumn Retrieve(int id)
        {
            using (dc = new IssueTrackerDBDataContext())
            {
                var list = from proj in dc.ProjectCustomTypeColumns
                           where proj.Id == id
                           select proj;

                if (list.Count<ProjectCustomTypeColumn>() > 0)
                {
                    ProjectCustomTypeColumn obj = list.First<ProjectCustomTypeColumn>();
                    return obj;
                }
                else
                    return null;
            }
        }

        public ProjectCustomTypeColumn Update(ProjectCustomTypeColumn projectCustomTypeColumn)
        {

            ProjectCustomTypeColumn temp = base.Clone<ProjectCustomTypeColumn>(projectCustomTypeColumn);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;
                dc.ProjectCustomTypeColumns.Attach(temp, true);

                try
                {
                    dc.SubmitChanges();

                    projectCustomTypeColumn = base.Clone<ProjectCustomTypeColumn>(temp);
                }
                catch (ChangeConflictException ccex)
                {
                    throw new ApplicationException("The project member record that you are trying to update maybe out-dated. Please refresh and try again.", ccex);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return projectCustomTypeColumn;

        }

        public void Delete(ProjectCustomTypeColumn projectCustomTypeColumn)
        {

            ProjectCustomTypeColumn temp = base.Clone<ProjectCustomTypeColumn>(projectCustomTypeColumn);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ProjectCustomTypeColumns.Attach(temp);

                dc.ProjectCustomTypeColumns.DeleteOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<ProjectCustomTypeColumn> List()
        {
            IEnumerable<ProjectCustomTypeColumn> returnList = null;

            using (dc = new IssueTrackerDBDataContext())
            {
                returnList = dc.ProjectCustomTypeColumns;

                if (returnList != null)
                    return returnList.ToList();
                else
                    return null;
            }
        }

        public IEnumerable<ProjectCustomTypeColumn> ListByReferenceId(int reference)
        {
            using (dc = new IssueTrackerDBDataContext())
            {
                var list = from proj in dc.ProjectCustomTypeColumns
                           where proj.ProjectCustomTypeId.Equals(reference)
                           select proj;

                return list;
            }
        }
   
    }
}
